const DrawUser = wx.cloud.database().collection('DrawUser')
const CreditRecord = wx.cloud.database().collection('CreditRecord')
const app = getApp()
const _ = wx.cloud.database().command
Page({

  /**
   * 页面的初始数据
   */
  data: {
    p: 1,
    bubbleType: '',
  },


  bubbleCredit() {
    let that = this
    if (!app.globalData.ischecked) {
      wx.showModal({
        title: '提示',
        content: '登录后即可乱点',
        showCancel: false,
        confirmText: "好的",
        cancelColor: 'cancelColor',
        confirmColor: '#00BFFF',
        success: function (res) {
          if (res.confirm) { }
        }

      })

    }
    else {
      var date = new Date()
      CreditRecord.add({
        data: {
          reason: "气泡加分",
          creditChange: 20,              //等等写
          createdAt: new Date(),        //时间
          date: String(date.getFullYear()) + "/" + String(date.getMonth()) + "/" + date.getDate(),
        }
      }).then(res => { console.log(res) })

      DrawUser.where({ _openid: app.globalData.userOpenId }).update({

        data: {
          credit: _.inc(20)
        },
        success: function (res) {
          console.log("用户积分已经加了20")
          console.log(res)
          DrawUser.where({ _openid: app.globalData.userOpenId }).get().then(re => {


            that.setData({
              //dataob:res.data,
              usercredit: re.data[0].credit,
              userticket: re.data[0].ticket,
            })
            //
            wx.setStorageSync('usercredit', re.data[0].credit)
            wx.setStorageSync('userticket', re.data[0].ticket)
          })
        },
      })

    }
  },


  //网上找的随机数生产器,要用就this.rand(number),number为想要的最大数字
  rand: function (number) {
    var today = new Date();
    var seed = today.getTime();
    seed = (seed * 9301 + 49297) % 233280; //为何使用这三个数?我也不知道
    seed = seed / (233280.0)
    return Math.ceil(seed * number);
  },


  randomCredit: function (e) {
    let that = this
    if (!app.globalData.ischecked) {   //登录状态判断
      wx.showModal({
        title: '提示',
        content: '登录后即可乱点',
        showCancel: false,
        confirmText: "好的",
        cancelColor: 'cancelColor',
        confirmColor: '#00BFFF',
        success: function (res) {
          if (res.confirm) { }
        }

      })

    }
    else {
      var date = new Date()
      var randresult = this.rand(3)       //生产随机数，加1到3分
      var reason = "任意加" + randresult + "积分"
      console.log(randresult)
      
      
      //访问次数爆了，就注释掉，别删

      CreditRecord.add({     //在CreditRecord添加一个记录，说明reason，加的分，时间
        data: {
          reason: reason,
          creditChange: randresult,
          createdAt: new Date(),
          date: String(date.getFullYear()) + "/" + String(date.getMonth()) + "/" + date.getDate(),
        }
      }).then(res => { console.log(res) })

      DrawUser.where({ _openid: app.globalData.userOpenId }).update({   //更新用户记录的数据

        data: {
          credit: _.inc(randresult)
        },
        success: function (res) {
          console.log("用户积分已经加了" + randresult + "积分")
          console.log(res)
          DrawUser.where({ _openid: app.globalData.userOpenId }).get().then(re => {//读取用户记录数据，
                                                                // 并将积分和票存到缓存，用于页面显示积分和票

            that.setData({

              usercredit: re.data[0].credit,
              userticket: re.data[0].ticket,
            })
            wx.setStorageSync('usercredit', re.data[0].credit)
            wx.setStorageSync('userticket', re.data[0].ticket)
          })

        },
      })

      //访问次数爆了，就注释掉
      let that = this
      //加上我的array,等等就写
      let array = ["+1", "+2", "+3"]
      let x = e.detail.x + 10
      let y = e.detail.y - 40
      console.log(e.detail.x)
      console.log(e.detail.y)

      //
      //
      if (randresult == 1) {
        that.setData({
          text_a: "+1",
          text_ax: x,
          text_ay: y,
          opacity_a: 1,
        })
        let timer_a = setInterval(function () {
          that.setData({
            opacity_a: 0,
            text_ay: y - 60,  //Y坐标移动移动，变成向上的动画一点
          })
          setTimeout(function () {
            that.setData({
              text_a: '', //赋值为空，即让它消失
            })
          }, 1000)
          clearInterval(timer_a);
        }, 200)
        return false;
      }
      if (randresult == 2) {
        that.setData({
          text_b: array[1],
          text_bx: x,
          text_by: y,
          opacity_b: 1,
        })
        let timer_b = setInterval(function () {
          that.setData({
            opacity_b: 0,
            text_by: y - 60,
          })
          setTimeout(function () {
            that.setData({
              text_b: '',
            })
          }, 1000)
          clearInterval(timer_b);
        }, 100)
        return false;
      }
      if (randresult == 3) {
        that.setData({
          text_c: array[2],
          text_cx: x,
          text_cy: y,
          opacity_c: 1,
        })
        let timer_c = setInterval(function () {
          that.setData({
            opacity_c: 0,
            text_cy: y - 60,
          })
          setTimeout(function () {
            that.setData({
              text_c: '',
            })
          }, 1000)
          clearInterval(timer_c);
        }, 100)
        return false;
      }

    }
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    let that = this
    var timer = setInterval(function () {
      var s = that.rand(7)
      console.log(s)
      if (s == 1) {
        that.setData({
          bubbleType1: "1"
        })
        setTimeout(() => {
          that.setData({
            bubbleType1: ''
          })

        }, 7000);
      }
      if (s == 2) {
        that.setData({
          bubbleType2: "2"
        })
        setTimeout(() => {
          that.setData({
            bubbleType2: ''
          })

        }, 7000);


      }
      if (s == 3) {
        that.setData({
          bubbleType3: "3"
        })
        setTimeout(() => {
          that.setData({
            bubbleType3: ''
          })

        }, 7000);

      }
      if (s == 4) {
        that.setData({
          bubbleType4: "4"
        })
        setTimeout(() => {
          that.setData({
            bubbleType4: ''
          })

        }, 7000);

      }
      if (s == 5) {
        that.setData({
          bubbleType5: "4"
        })
        setTimeout(() => {
          that.setData({
            bubbleType5: ''
          })

        }, 7000);

      }
      if (s == 6) {
        that.setData({
          bubbleType6: "4"
        })
        setTimeout(() => {
          that.setData({
            bubbleType6: ''
          })

        }, 7000);

      }
      if (s == 7) {
        that.setData({
          bubbleType7: "4"
        })
        setTimeout(() => {
          that.setData({
            bubbleType7: ''
          })

        }, 7000);

      }
    }, 9000)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.globalData.ischecked = wx.getStorageSync('ischecked')
    app.globalData.userOpenId = wx.getStorageSync('app.globalData.userOpenId')
    DrawUser.where({ _openid: app.globalData.userOpenId }).get().then(res => {


      this.setData({
        //dataob:res.data,
        usercredit: res.data[0].credit,
        userticket: res.data[0].ticket,
      })
      //
      wx.setStorageSync('usercredit', res.data[0].credit)
      wx.setStorageSync('userticket', res.data[0].ticket)
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})